package com.ihome.android.common;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.ihome.android.dl.core.DownloadTask;
import com.ihome.android.dl.util.DLFileInfo;
import com.ihome.android.market2.info.AccountInfo;
import com.ihome.android.util.Util;
import com.ihome.android.util.log.LogUtil;

public class ConfigUtil {
	private static final String				TAG						= ConfigUtil.class.getSimpleName();
	public static boolean					isDebug					= true;
	/** --------------------------------- 正式平台 --------------------------------- */
	/** 升级检查地址 */
	public static final String				CHECKUPDATE_URL			= "http://appStoreRrc.cnitv.net:8090/tv/updater2";
	/** 升级结果上报地址 */
	public static final String				UPDATERESULTREPORT_URL	= "http://appStoreRrc.cnitv.net:8090/tv/updaterlog";
	/** 应用商城接口地址(包括数据采集接口) */
	public static final String				SERVER_URL				= "http://192.168.1.110:8080/MeAmsIntf/YueXueTangReq";
	/** 应用专题地址 */
	public final static String				DefaultThemeUrl			= "http://appStoreRrc.cnitv.net:8088/ipc.doc/Docs/special/meishi/spec.html";
	/** --------------------------------- 测试平台 --------------------------------- */
	/** 升级检查地址 */
//		public static final String				CHECKUPDATE_URL			= "http://221.228.17.201:8080/tv/updater2";
//	/** 升级结果上报地址 */
//		public static final String				UPDATERESULTREPORT_URL	= "http://221.228.17.201:8080/tv/updaterlog";
//	/** 应用商城接口地址(包括数据采集接口) */
//		public static final String				SERVER_URL				= "http://221.228.17.201:8080/sht_android_mall/client";
//	/** 应用专题地址 */
//		public final static String				DefaultThemeUrl			= "http://221.228.17.201:8080/ipc.doc/Docs/special/meishi/spec.html";

	/** ----------------------------- 初始化环境参数 --------------------------------- */
	public static String					IMG_PATH				= null;
	public static String					FILE_PATH				= null;
	public static String					LOG_PATH				= null;

	public static String					IPTV_SPID				= "";
	public static int						APPMARKETID				= 1;
	public static String					USERAPPGROUP			= "1";
	/** 当前Market用户信息 */
	public static AccountInfo				accountInfo				= null;
	/** 当前MarketSession */
	public static String					SessionKey				= null;
	
	/** 添加采集上报文件大小 单位默认为KB */
	public static String					dataGatherSize			= "256";

	public static String					APPSTORE_KEY			= "com.ihome.android.market2";
	public static String					MINSHENG_KEY			= "com.ihome.android.minsheng";
	/*** 需被过滤无需在已安装列表中显示的应用 */
	public static String					CTC_ZHIHUI_KEY			= "com.ctc.bindcode";
	public static String					MEDIARENDER_KEY			= "com.telecom.mediarender";
	/** 第三方应用签名缓存 */
	public static HashMap<String, String>	appSignList				= new HashMap<String, String>();
	public static String					VIDEO_URL_KEY			= "videoUrl";

	/**视频播放相关**/
	public final static int		PLAY_TYPE_NONE		= 0;
	public final static int		PLAY_TYPE_LIVE		= 0x01;
	public final static int		PLAY_TYPE_VOD		= 0x02;
	public final static String	PLAY_TYPE_KEY		= "PLAY_TYPE";
	public final static String	PLAY_URL_KEY		= "PLAY_URL";
	public final static int STEP_FAST_REWIND_LIVE = 30;
	public final static int STEP_FAST_REWIND_VOD = 30000;
	public final static int STEP_FAST_REWIND_NONE = 0;
	
	
	/** 初始化文件夹路径 */
	public static void InitPath(Context ctx) {
		File temFile = Environment.getExternalStorageDirectory();

		if (temFile != null && temFile.canWrite() && Util.getAvailableExternalMemorySize() > 0) {
			IMG_PATH = Environment.getExternalStorageDirectory().getPath() + "/Academy/Image/";
			FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/Academy/Downloads/";
			LOG_PATH = Environment.getExternalStorageDirectory().getPath() + "/Academy/logs/";
		}
		else {
			IMG_PATH = ctx.getFilesDir() + "/Image/";
			FILE_PATH = ctx.getFilesDir().getAbsolutePath() + File.separator;
			LOG_PATH = ctx.getFilesDir().getAbsolutePath() + File.separator + "logs" + File.separator;
		}

		new File(IMG_PATH).mkdirs();
		new File(FILE_PATH).mkdirs();
		new File(LOG_PATH).mkdirs();

		//LogUtil.i(TAG, "IMG_PATH-->" + IMG_PATH + "\n FILEPATH-->" + FILE_PATH);
	}

	/** --------------------------------- 下载数据 --------------------------------- */
	public static final String	DL_PREFERENCE_NAME	= "com.academy.download";

	public static SharedPreferences getPreferences(Context context) {
		return context.getSharedPreferences(DL_PREFERENCE_NAME, Context.MODE_PRIVATE);
	}

	public static HashMap<String, String> getAll(Context context) {
		SharedPreferences preferences = getPreferences(context);

		HashMap<String, String> allDlInfo = new HashMap<String, String>();
		Map<String, ?> allContent = preferences.getAll();
		for (Map.Entry<String, ?> entry : allContent.entrySet()) {
			String key = entry.getKey();
			LogUtil.d(TAG, "key -->  " + key);

			if (entry.getValue() instanceof String) {
				allDlInfo.put(entry.getKey(), (String) entry.getValue());
			}
		}
		return allDlInfo;
	}

	public static String getString(Context context, String key) {
		SharedPreferences preferences = getPreferences(context);
		if (preferences != null)
			return preferences.getString(key, "");
		else
			return "";
	}

	public static void setString(Context context, String key, String value) {
		SharedPreferences preferences = getPreferences(context);
		if (preferences != null) {
			Editor editor = preferences.edit();
			editor.putString(key, value);
			editor.commit();
		}
	}

	public static void clearString(Context context, String key) {
		SharedPreferences preferences = getPreferences(context);
		if (preferences != null) {
			Editor editor = preferences.edit();
			editor.remove(key);
			editor.commit();
		}
	}

	public static final int		URL_COUNT	= DownloadTask.MAX_DOWNLOAD_THREAD_COUNT;
	public static final String	KEY_URL		= "url";

	public static void storeURL(Context context, String index, DLFileInfo dLFileInfo) {
		String s = new Gson().toJson(dLFileInfo);
		setString(context, KEY_URL + index, s);
	}

	public static void clearURL(Context context, String index) {
		//setString(context, KEY_URL + index, "");
		clearString(context, KEY_URL + index);
	}

	public static String getURL(Context context, String index) {
		return getString(context, KEY_URL + index);
	}

	public static List<DLFileInfo> getURLArray(Context context) {
		List<DLFileInfo> urlList = new ArrayList<DLFileInfo>();

		HashMap<String, String> allDlInfo = getAll(context);
		for (Map.Entry<String, String> entry : allDlInfo.entrySet()) {

			String key = entry.getKey();
			String value = entry.getValue();
			LogUtil.d(TAG, "getURLArray() --> key = " + key);
			LogUtil.d(TAG, "getURLArray() --> value = " + value);

			if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
				urlList.add(new Gson().fromJson(value, DLFileInfo.class));
			}
		}

		return urlList;
	}
}
